package com.home.controller.system.basic;


import com.alibaba.druid.util.jdbc.ResultSetBase;
import com.home.entity.Menu;
import com.home.entity.RespBean;
import com.home.entity.Role;
import com.home.service.MenuService;
import com.home.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/basic/permiss")
public class PermissController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    @GetMapping("/")
    public List<Role> getAllRoles() {
        List<Role> lists = roleService.getAllRoles();
        return lists;
    }

    @PostMapping("/role")
    public RespBean addRole(@RequestBody Role role) {
        if (ObjectUtils.isEmpty(role)) {
            return null;
        }
        if (roleService.addRole(role) == 1) {
            return RespBean.ok("添加成功");
        }
        return RespBean.error("添加失败");
    }


    @PutMapping("/role")
    public RespBean updateRole(@RequestBody Role role) {
        if (ObjectUtils.isEmpty(role)) {
            return RespBean.error("参数有误，无法更新");
        }
        if (roleService.updateRole(role) == 1) {
            return RespBean.ok("更新成功");
        }
        return RespBean.error("更新失败");
    }

    @GetMapping("/menus")
    public List<Menu> getAllMenus() {
        List<Menu> lists = menuService.getAllMenus();
        return lists;
    }

    @GetMapping("/mids/{rid}")
    public List<Integer> getMidsByRid(@PathVariable Integer rid) {
        List<Integer> lists = menuService.getMidsByRid(rid);
        return lists;
    }

    @PutMapping("/")
    public RespBean updateMenuRole(Integer rid, Integer[] mids) {
        if (menuService.updateMenuRole(rid, mids)) {
            return RespBean.ok("更新成功");
        }
        return RespBean.error("更新失败");
    }

    @DeleteMapping("/role/{rid}")
    public RespBean deleteRoleById(@PathVariable Integer rid) {
        System.out.println("rid=" + rid);
        if (roleService.deleteRoleById(rid) == 1) {
            return RespBean.ok("删除成功!");
        }
        return RespBean.error("删除失败!");
    }

}
